fn main() {
    let mut s1 = String::from("hello");

    let len = calculate_length(&s1);

    println!("The length of '{}' is {}.", s1, len);

    change(&mut s1);

    println!("After change s1 is {s1}");

    let r1 = &s1;
    let r2 = &s1;
    println!("{r1} and {r2}");

    let r3 = &mut s1;
    println!("{r3}");

    println!("{}", new());

    let hello = &s1[..5];
    let world = &s1[6..];
    println!("{hello} {world}");

    println!("first word: {}", first_word("hello rust"));
}

fn calculate_length(s: &String) -> usize {
    s.len()
}

fn change(s: &mut String) {
    s.push_str(" world");
}

fn new() -> String /*&String */ {
    let s = String::from("new");
    //&s
    s
}

fn first_word(s: &str) -> &str {
    let bytes = s.as_bytes();

    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return &s[0..i];
        }
    }

    &s[..]
}
